var express = require('express');
var router = express.Router();
const Quert = require('../utils/db.js')
/* GET home page. */
router.get('/', function (req, res, next) {
  res.render('index', { title: 'Express123' });
});

router.get('/banners', async (req, res, next) => {
  //sql语句 
  let sql = `SELECT id,title,isshow, CONCAT('http://localhost:3000/uploads/banners/',image_src) AS image_src FROM e_banners WHERE isshow=1`
  let { err, result } = await Quert(sql)

  //出现错误时显示
  if (err) {
    // res.send({
    //   mag: '出现错误咯',
    //   status: 500
    // })

    next({
      mag: '出现错误咯',
      status: 500
    })
    return
  }
  let data = {
    msg: "ok",
    status: 200,
    result
  }
  res.send(data)
})


router.get('/courseinfo', async (req, res, next) => {

  let { type = 1 } = req.query
  let size = type == 1 ? 8 : 6
  let sql = `SELECT id,cid,title,price,CONCAT('http://localhost:3000/uploads/courses/',image_src) AS image_src,type_name,area_name,subject_name,type FROM e_course WHERE type = ${type} ORDER BY id DESC LIMIT ${size}`

  let { err, result } = await Quert(sql)
  if (err) {
    // res.send({
    //   mag: '出现错误咯',
    //   status: 500
    // })
    next({
      mag: '出现错误咯',
      status: 500
    })
    return
  }
  let data = {
    msg: "ok",
    status: 200,
    result
  }
  res.send(data)

})
module.exports = router;
